/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

#include "main_ability_slice.h"
#include "ability_env.h"
#include "ability_manager.h"
#include "components/ui_label.h"
#include "components/ui_label_button.h"
#include "components/ui_image_view.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include<stdio.h>
#include "file.h"
#include "securec.h"

namespace OHOS
{
REGISTER_AS(MainAbilitySlice)

constexpr static int ROOT_VIEW_POSITION_X = 0;
constexpr static int ROOT_VIEW_POSITION_Y = 0;
constexpr static int ROOT_VIEW_WIDTH = 960;
constexpr static int ROOT_VIEW_HEIGHT = 480;
static constexpr uint16_t MAX_PATH_LENGTH = 512;
static const char* const BACK_ICON_PATH = "/controlCar/assets/entry/resources/base/media/ic_back.png";
static const char* const LEFT_UP_PATH = "/controlCar/assets/entry/resources/base/media/left_up.png";
static const char* const UP_PATH = "/controlCar/assets/entry/resources/base/media/up.png";
static const char* const RIGHT_UP_PATH = "/controlCar/assets/entry/resources/base/media/right_up.png";
static const char* const LEFT_PATH = "/controlCar/assets/entry/resources/base/media/left.png";
static const char* const STOP_PATH = "/controlCar/assets/entry/resources/base/media/stop.png";
static const char* const RIGHT_PATH = "/controlCar/assets/entry/resources/base/media/right.png";
static const char* const LEFT_DOWN_PATH = "/controlCar/assets/entry/resources/base/media/left_down.png";
static const char* const DOWN_PATH = "/controlCar/assets/entry/resources/base/media/down.png";
static const char* const RIGHT_DOWN_PATH = "/controlCar/assets/entry/resources/base/media/right_down.png";
static char g_backIconAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_left_upAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_upAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_right_upAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_leftAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_stopAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_rightAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_left_downAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_downAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_right_downAbsolutePath[MAX_PATH_LENGTH] = {0};
void MainAbilitySlice::OnStart(const Want &want)
{
    printf("MainAbilitySlice::OnStart\n");
    AbilitySlice::OnStart(want);
    const char* pathHeader = GetSrcPath();
    if(sprintf_s(g_backIconAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,BACK_ICON_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backIconAbsolutePath error");
        return;
    }
    if(sprintf_s(g_left_upAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,LEFT_UP_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround00AbsolutePath error");
        return;
    }
    if(sprintf_s(g_upAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,UP_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround01AbsolutePath error");
        return;
    }
    if(sprintf_s(g_right_upAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,RIGHT_UP_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround02AbsolutePath error");
        return;
    }
    if(sprintf_s(g_leftAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,LEFT_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround00AbsolutePath error");
        return;
    }
    if(sprintf_s(g_stopAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,STOP_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround01AbsolutePath error");
        return;
    }
    if(sprintf_s(g_rightAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,RIGHT_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround02AbsolutePath error");
        return;
    }
    if(sprintf_s(g_left_downAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,LEFT_DOWN_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround00AbsolutePath error");
        return;
    }
    if(sprintf_s(g_downAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,DOWN_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround01AbsolutePath error");
        return;
    }
    if(sprintf_s(g_right_downAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,RIGHT_DOWN_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround02AbsolutePath error");
        return;
    }
    backIcon = new UIImageView();
    backIcon->SetPosition(0, 0);
    backIcon->SetSrc(g_backIconAbsolutePath);
    backIcon->SetTouchable(true);
    backIcon->Resize(40,40);
    auto onClick = [this] (UIView& view, const Event& event) -> bool {
        TerminateAbility();
        return true;
    };
    backIconListener = new EventListener(onClick, nullptr);
    backIcon->SetOnClickListener(backIconListener);
    
    //设置置顶label
    label_title = new UILabel();
    label_title->SetPosition(280,0);
    label_title->Resize(500,40);
    label_title->SetTextColor(Color::Red());
    label_title->SetText("欢迎各位大佬使用本智能小车遥控器，希望各位大佬能玩的开心！");
    label_title->SetFont("SourceHanSansSC-Regular.otf",30);
    label_title->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);

    //设置显示遥控器模式状态label
    label_remote_state = new UILabel();
    label_remote_state->SetPosition(10,40);
    label_remote_state->Resize(300,40);
    label_remote_state->SetTextColor(Color::Green());
    label_remote_state->SetText("当前模式：基础模式");
    label_remote_state->SetFont("SourceHanSansSC-Regular.otf",20);

    //设置label
    label = new UILabel();
    label->SetPosition(300,40);
    label->SetStyle(STYLE_BACKGROUND_COLOR,Color::Gray().full);
    label->Resize(350,40);
    label->SetText("当前小车连接状态：正在检测中...");
    label->SetTextColor(Color::Green());
    label->SetFont("SourceHanSansSC-Regular.otf", 20);

    //设置连接小车按钮
    bt_connect = new UILabelButton();
    bt_connect->SetPosition(700,40,100,40);
    bt_connect->SetTextColor(Color::Green());
    bt_connect->SetText("连接小车");

    //设置断开连接按钮
    bt_disconnect = new UILabelButton();
    bt_disconnect->SetPosition(850,40,100,40);
    bt_disconnect->SetTextColor(Color::Green());
    bt_disconnect->SetText("断开连接");
    
    //设置滑动容器组件UISwipeView
    swipe = new UISwipeView(UISwipeView::HORIZONTAL);
    swipe->SetPosition(30,80);
    swipe->Resize(900,400);
    swipeListener = new mySwipeListener(swipe,label_remote_state);
    swipe->SetOnSwipeListener(swipeListener);

    //设置第一个swipe界面
    gridlayout = new GridLayout();
    gridlayout->SetPosition(0,0,900,400);
    gridlayout->SetRows(3);
    gridlayout->SetCols(5);
    gridlayout->SetLayoutDirection(LAYOUT_HOR);

    bt_left_one = new UILabelButton();
    bt_left_one->SetPosition(0,0,100,100);
    bt_left_one->SetVisible(false);

    bt_left_up = new UILabelButton();
    bt_left_up->SetPosition(0,0,100,100);
    bt_left_up->SetImageSrc(g_left_upAbsolutePath,g_left_upAbsolutePath);
    bt_left_up->SetImagePosition(0,0);
    bt_left_up->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::White().full,UIButton::RELEASED);
    bt_left_up->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::RELEASED);
    bt_left_up->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::Red().full,UIButton::UIButton::PRESSED);
    bt_left_up->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::UIButton::PRESSED);

    bt_up = new UILabelButton();
    bt_up->SetPosition(0,0,100,100);
    bt_up->SetImageSrc(g_upAbsolutePath,g_upAbsolutePath);
    bt_up->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::White().full,UIButton::RELEASED);
    bt_up->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::RELEASED);
    bt_up->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::Red().full,UIButton::UIButton::PRESSED);
    bt_up->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::UIButton::PRESSED);

    bt_right_up = new UILabelButton();
    bt_right_up->SetPosition(0,0,100,100);
    bt_right_up->SetImageSrc(g_right_upAbsolutePath,g_right_upAbsolutePath);
    bt_right_up->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::White().full,UIButton::RELEASED);
    bt_right_up->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::RELEASED);
    bt_right_up->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::Red().full,UIButton::UIButton::PRESSED);
    bt_right_up->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::UIButton::PRESSED);

    bt_right_one = new UILabelButton();
    bt_right_one->SetPosition(0,0,100,100);
    bt_right_one->SetVisible(false);

    bt_left_two = new UILabelButton();
    bt_left_two->SetPosition(0,0,100,100);
    bt_left_two->SetVisible(false);

    bt_left = new UILabelButton();
    bt_left->SetPosition(0,0,100,100);
    bt_left->SetImageSrc(g_leftAbsolutePath,g_leftAbsolutePath);
    bt_left->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::White().full,UIButton::RELEASED);
    bt_left->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::RELEASED);
    bt_left->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::Red().full,UIButton::UIButton::PRESSED);
    bt_left->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::UIButton::PRESSED);

    bt_stop = new UILabelButton();
    bt_stop->SetPosition(0,0,100,100);
    bt_stop->SetStyle(STYLE_BORDER_RADIUS,0);
    bt_stop->SetImageSrc(g_stopAbsolutePath,g_stopAbsolutePath);
    bt_stop->SetStyle(STYLE_BACKGROUND_COLOR,Color::White().full);

    bt_right = new UILabelButton();
    bt_right->SetPosition(0,0,100,100);
    bt_right->SetImageSrc(g_rightAbsolutePath,g_rightAbsolutePath);
    bt_right->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::White().full,UIButton::RELEASED);
    bt_right->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::RELEASED);
    bt_right->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::Red().full,UIButton::UIButton::PRESSED);
    bt_right->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::UIButton::PRESSED);

    bt_right_two = new UILabelButton();
    bt_right_two->SetPosition(0,0,100,100);
    bt_right_two->SetVisible(false);

    bt_left_three = new UILabelButton();
    bt_left_three->SetPosition(0,0,100,100);
    bt_left_three->SetVisible(false);

    bt_left_down = new UILabelButton();
    bt_left_down->SetPosition(0,0,100,100);
    bt_left_down->SetImageSrc(g_left_downAbsolutePath,g_left_downAbsolutePath);
    bt_left_down->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::White().full,UIButton::RELEASED);
    bt_left_down->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::RELEASED);
    bt_left_down->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::Red().full,UIButton::UIButton::PRESSED);
    bt_left_down->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::UIButton::PRESSED);

    bt_down = new UILabelButton();
    bt_down->SetPosition(0,0,100,100);
    bt_down->SetImageSrc(g_downAbsolutePath,g_downAbsolutePath);
    bt_down->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::White().full,UIButton::RELEASED);
    bt_down->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::RELEASED);
    bt_down->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::Red().full,UIButton::UIButton::PRESSED);
    bt_down->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::UIButton::PRESSED);

    bt_right_down = new UILabelButton();
    bt_right_down->SetPosition(0,0,100,100);
    bt_right_down->SetImageSrc(g_right_downAbsolutePath,g_right_downAbsolutePath);
    bt_right_down->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::White().full,UIButton::RELEASED);
    bt_right_down->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::RELEASED);
    bt_right_down->SetStyleForState(STYLE_BACKGROUND_COLOR,Color::Red().full,UIButton::UIButton::PRESSED);
    bt_right_down->SetStyleForState(STYLE_BORDER_RADIUS,0,UIButton::UIButton::PRESSED);

    bt_right_three = new UILabelButton();
    bt_right_three->SetPosition(0,0,100,100);
    bt_right_three->SetVisible(false);

    gridlayout->Add(bt_left_one);
    gridlayout->Add(bt_left_up);
    gridlayout->Add(bt_up);
    gridlayout->Add(bt_right_up);
    gridlayout->Add(bt_right_one);
    gridlayout->Add(bt_left_two);
    gridlayout->Add(bt_left);
    gridlayout->Add(bt_stop);
    gridlayout->Add(bt_right);
    gridlayout->Add(bt_right_two);
    gridlayout->Add(bt_left_three);
    gridlayout->Add(bt_left_down);
    gridlayout->Add(bt_down);
    gridlayout->Add(bt_right_down);
    gridlayout->Add(bt_right_three);
    gridlayout->LayoutChildren();    

    label_second = new UILabel();
    label_second->Resize(900,400);
    label_second->SetText("预留给智能小车的第二种玩法模式，有待开发！");
    label_second->SetAlign(TEXT_ALIGNMENT_CENTER,TEXT_ALIGNMENT_CENTER);
    label_second->SetFont("SourceHanSansSC-Regular.otf", 50);

    label_thrid = new UILabel();
    label_thrid->Resize(900,400);
    label_thrid->SetText("预留给智能小车的第三种玩法模式，有待探索！");
    label_thrid->SetAlign(TEXT_ALIGNMENT_CENTER,TEXT_ALIGNMENT_CENTER);
    label_thrid->SetFont("SourceHanSansSC-Regular.otf", 50);

    swipe->Add(gridlayout);
    swipe->Add(label_second);
    swipe->Add(label_thrid);
    swipe->SetLoopState(true);

    rootView_ = RootView::GetWindowRootView();
    rootView_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y);
    rootView_->Resize(ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT);
    rootView_->Add(backIcon);
    rootView_->Add(label_title);
    rootView_->Add(label_remote_state);
    rootView_->Add(bt_connect);
    rootView_->Add(bt_disconnect);
    rootView_->Add(label);
    rootView_->Add(swipe);

    SetUIContent(rootView_);
}

void MainAbilitySlice::OnStop()
{
    printf("MainAbilitySlice::OnStop\n");
    AbilitySlice::OnStop();
    if (backIconListener != nullptr) {
        delete backIconListener;
        backIconListener = nullptr;
    }
    if (backIcon != nullptr) {
        delete backIcon;
        backIcon = nullptr;
    }
    if (label != nullptr) {
        delete label;
        label = nullptr;
    }
    if (label_title != nullptr){
        delete label_title;
        label = nullptr;
    }
    if (label_remote_state != nullptr){
        delete label_remote_state;
        label_remote_state = nullptr;
    }
    if (label_second != nullptr){
        delete label_second;
        label_second = nullptr;
    }
    if (label_thrid != nullptr){
        delete label_thrid;
        label_thrid = nullptr;
    }
    if (bt_connect != nullptr){
        delete bt_connect;
        bt_connect = nullptr;
    }
    if (bt_disconnect != nullptr){
        delete bt_disconnect;
        bt_disconnect = nullptr;
    }
    if (bt_left_one != nullptr){
        delete bt_left_one;
        bt_left_one = nullptr;
    }
    if (bt_left_up != nullptr ) {
        delete bt_left_up;
        bt_left_up = nullptr;
    }
    if (bt_up != nullptr ){
        delete bt_up;
        bt_up = nullptr;
    }
    if (bt_right_up != nullptr ){
        delete bt_right_up;
        bt_right_up = nullptr;
    }
    if (bt_right_one != nullptr){
        delete bt_right_one;
        bt_right_one = nullptr;
    }
    if (bt_left_two != nullptr){
        delete bt_left_two;
        bt_left_two = nullptr;
    }
    if (bt_left != nullptr){
        delete bt_left;
        bt_left = nullptr;
    }
    if (bt_stop != nullptr){
        delete bt_stop;
        bt_stop = nullptr;
    }
    if (bt_right != nullptr){
        delete bt_right;
        bt_right = nullptr;
    }
    if (bt_right_two != nullptr){
        delete bt_right_two;
        bt_right_two = nullptr;
    }
    if (bt_left_three != nullptr){
        delete bt_left_three;
        bt_left_three = nullptr;
    }
    if (bt_left_down != nullptr){
        delete bt_left_down;
        bt_left_down = nullptr;
    }
    if (bt_down != nullptr){
        delete bt_down;
        bt_down = nullptr;
    }
    if (bt_right_down != nullptr){
        delete bt_right_down;
        bt_right_down = nullptr;
    }
    if (bt_right_three != nullptr){
        delete bt_right_three;
        bt_right_three = nullptr;
    }
    if (gridlayout != nullptr){
        delete gridlayout;
        gridlayout = nullptr;
    }
    if (swipe != nullptr){
        delete swipe;
        swipe = nullptr;
    }
    if (swipeListener != nullptr){
        delete swipeListener;
        swipeListener = nullptr;
    }
    if (rootView_ != nullptr){
        RootView::DestoryWindowRootView(rootView_);
        rootView_=nullptr;
    }
}
} // namespace OHOS
